<template>
  <div>
    <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="120px" class="form-style">
      <aside>Create or Edit a Memory Space.</aside>
      <el-form-item label="Category" prop="category">
        <el-select v-model="form.category" filterable clearable class="filter-item" placeholder="Please select">
          <el-option v-for="item in categories" :key="item.code" :label="item.text" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="Space Name" prop="spaceName">
        <el-input v-model="form.spaceName" clearable maxlength="150" show-word-limit />
      </el-form-item>
      <el-form-item label="Alias Name" prop="aliasName">
        <el-input v-model="form.aliasName" clearable maxlength="150" show-word-limit />
      </el-form-item>
      <el-form-item label="Purpose" prop="purpose">
        <el-input v-model="form.purpose" clearable maxlength="150" show-word-limit />
      </el-form-item>
      <el-form-item label="Password" prop="password">
        <el-input v-model="form.password" clearable maxlength="150" show-word-limit />
      </el-form-item>
      <el-form-item label="Used" prop="free">
        <el-input-number v-model="form.freex" :precision="1" :step="5" />GB
      </el-form-item>
      <el-form-item label="Capacity" prop="capacity">
        <el-input-number v-model="form.capacityx" :precision="1" :step="5" />GB
      </el-form-item>
      <el-form-item label="State" prop="state">
        <el-input v-model="form.state" :autosize="{ minRows: 3, maxRows: 5 }" type="textarea" placeholder="Please input" />
      </el-form-item>
      <el-form-item>
        <el-button v-waves v-loading="loading" type="primary" @click="submitForm">Save Now</el-button>
        <el-button v-show="!isEdit" @click="reset">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { createOrUpdateMemorySpace } from '@/api/usr/memory-space'
import waves from '@/directive/waves' // waves directive

const defaultForm = {
  spaceName: '',
  aliasName: '',
  purpose: '',
  state: '',
  password: '',
  freex: 0,
  capacityx: 0,
  free: 0,
  capacity: 0,
  category: '',
  encrypted: false
}

export default {
  name: 'MemorySpace',
  directives: { waves },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      rules: {
        spaceName: [{ required: true, message: 'Space Name is required', trigger: 'blur' }],
        category: [{ required: true, message: 'category is required', trigger: 'change' }],
        purpose: [{ required: true, message: 'purpose is required', trigger: 'blur' }]
      },
      loading: false
    }
  },
  computed: {
    ...mapGetters(['uid']),
    categories() {
      return this.$store.getters.dictionaries.filter((category) => category.dictGroup === 'space_category')
    }
  },
  created() {
    if (this.isEdit) {
      const row = this.$route.params
      this.form = Object.assign(
        {},
        row,
        { category: row.category.toString() },
        { freex: row.free / 1024 / 1024 },
        { capacityx: row.capacity / 1024 / 1024 },
        { createAt: undefined, modifyAt: undefined }
      )
    } else {
      this.form = Object.assign({}, defaultForm, { uid: this.uid })
    }
  },
  methods: {
    submitForm() {
      console.log(this.form)
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.form.capacity = this.form.capacityx * 1024 * 1024
          this.form.free = this.form.freex * 1024 * 1024
          createOrUpdateMemorySpace(this.form)
            .then((response) => {
              this.$notify({
                title: 'Success',
                message: 'save successfully',
                type: 'success',
                duration: 2000
              })
              if (!this.isEdit) {
                this.$refs['dataForm'].resetFields()
              }
            })
            .catch(() => {
              this.loading = false
            })
          this.loading = false
        } else {
          return false
        }
      })
    },
    reset() {
      this.$refs['dataForm'].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.form-style {
  margin-top: 1%;
  width: 88%;
  margin-left: 6%;
}
</style>
